//
//  YGBodyDataViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/4/7.
//

#import "YGBodyDataViewController.h"
#import "YGBodyDataBLL.h"
#import "YGFormTableView.h"
#import "YGBodyDataTableViewCell.h"
#import "YGBodyHistoryViewController.h"
@interface YGBodyDataViewController ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *genderLabel;
@property (nonatomic, strong) UIButton *maleBtn;
@property (nonatomic, strong) UILabel *maleLabel;
@property (nonatomic, strong) UIButton *femaleBtn;
@property (nonatomic, strong) UILabel *femaleLabel;

@property (nonatomic, strong) UILabel *ageLabel;
@property (nonatomic, strong) UILabel *heightLabel;
@property (nonatomic, strong) UILabel *weightLabel;
@property (nonatomic, strong) UILabel *unitAgeLabel;
@property (nonatomic, strong) UILabel *unitHeightLabel;
@property (nonatomic, strong) UILabel *unitWeightLabel;

@property (nonatomic, strong) UITextField *ageTextField;
@property (nonatomic, strong) UITextField *heightTextField;
@property (nonatomic, strong) UITextField *weightTextField;
@property (nonatomic, strong) UIButton *countBtn;
@property (nonatomic, strong) UIButton *historyBtn;

@property (nonatomic, assign) NSString *sex;

@property (nonatomic, strong) UIScrollView  *contentScrollView;


@property (nonatomic, strong) UILabel *daixieLabel;
@property (nonatomic, strong) UILabel *bmLabel;
@property (nonatomic, strong) UILabel *heahlthLabel;
@property (nonatomic, strong) UILabel *xinlvLabel;
@property (nonatomic, strong) UILabel *beLabel;
@property (nonatomic, strong) UIView  *contentView;

@property (nonatomic, strong) YGFormTableView  *fromView;

@end

@implementation YGBodyDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"自测工具"];
    //设置导航栏属性
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x37C8D7);
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : UIColorFromRGB(0xffffff)}];
    self.sex = @"1";
    [self initView];
    [self initconstraints];
    
    [self.fromView configureData:[self getFormData]];
}

static NSString *defaultDXContent = @"您的基础代谢率:";
static NSString *defaultBMContent = @"您的BM指数为:";
static NSString *defaultJKContent = @"您的健康体质范围为:";
static NSString *defaultXLContent = @"您的中低强度运动心率是:";
static NSString *defaultBEContent = @"您的BEEE值为:";

#pragma private

-(void)initView{
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.genderLabel];
    [self.view addSubview:self.maleBtn];
    [self.view addSubview:self.maleLabel];
    [self.view addSubview:self.femaleBtn];
    [self.view addSubview:self.femaleLabel];
    [self.view addSubview:self.ageLabel];
    [self.view addSubview:self.ageTextField];
    [self.view addSubview:self.unitAgeLabel];
    [self.view addSubview:self.heightLabel];
    [self.view addSubview:self.heightTextField];
    [self.view addSubview:self.unitHeightLabel];
    [self.view addSubview:self.weightLabel];
    [self.view addSubview:self.weightTextField];
    [self.view addSubview:self.unitWeightLabel];
    [self.view addSubview:self.countBtn];
    [self.view addSubview:self.historyBtn];
    
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.daixieLabel];
    [self.contentView addSubview:self.bmLabel];
    [self.contentView addSubview:self.heahlthLabel];
    [self.contentView addSubview:self.xinlvLabel];
    [self.contentView addSubview:self.beLabel];

    [self.view addSubview:self.fromView];
}

-(void)initconstraints{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.view).offset(110);
            make.left.mas_equalTo(self.view).offset(10);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(220);
    }];
    
    [self.genderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.view).offset(10);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(40);
    }];
    
    [self.maleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.genderLabel.mas_centerY);
            make.left.mas_equalTo(self.genderLabel.mas_right).offset(20);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
    }];
    
    [self.maleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.maleBtn.mas_centerY);
            make.left.mas_equalTo(self.maleBtn.mas_right).offset(5);
            make.width.mas_equalTo(12);
            make.height.mas_equalTo(14);
    }];
    
    [self.femaleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.genderLabel.mas_centerY);
            make.left.mas_equalTo(self.maleLabel.mas_right).offset(20);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
            
    }];
    
    [self.femaleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.femaleBtn.mas_centerY);
            make.left.mas_equalTo(self.femaleBtn.mas_right).offset(5);
            make.width.mas_equalTo(12);
            make.height.mas_equalTo(14);
    }];
    
    [self.ageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.genderLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.genderLabel);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(40);
    }];
    
    [self.heightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.ageLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.ageLabel);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(40);
    }];
    
    [self.weightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.heightLabel.mas_bottom).offset(20);
            make.left.mas_equalTo(self.heightLabel);
            make.height.mas_equalTo(15);
            make.width.mas_equalTo(40);
    }];
    
    [self.ageTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.ageLabel.mas_centerY);
            make.left.mas_equalTo(self.ageLabel.mas_right).offset(20);
            make.width.mas_equalTo(134);
            make.height.mas_equalTo(26);
    }];
    
    [self.unitAgeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.ageTextField.mas_centerY);
            make.left.mas_equalTo(self.ageTextField.mas_right).offset(5);
            make.width.mas_equalTo(12);
            make.height.mas_equalTo(14);
    }];
    
    [self.heightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.heightLabel.mas_centerY);
            make.left.mas_equalTo(self.heightLabel.mas_right).offset(20);
            make.width.mas_equalTo(134);
            make.height.mas_equalTo(26);
    }];
    
    [self.unitHeightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.heightTextField.mas_centerY);
            make.left.mas_equalTo(self.heightTextField.mas_right).offset(5);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(14);
    }];
    
    [self.weightTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.weightLabel.mas_centerY);
            make.left.mas_equalTo(self.weightLabel.mas_right).offset(20);
            make.width.mas_equalTo(134);
            make.height.mas_equalTo(26);
    }];
    
    [self.unitWeightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.weightTextField.mas_centerY);
            make.left.mas_equalTo(self.weightTextField.mas_right).offset(5);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(14);
    }];
    
    [self.countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.weightTextField.mas_bottom).offset(30);
            make.left.mas_equalTo(self.view).offset(10);
            make.right.mas_equalTo(self.view).offset(-10);
            make.height.mas_equalTo(40);
    }];
    
    [self.historyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.countBtn.mas_bottom).offset(20);
            make.right.mas_equalTo(self.countBtn.mas_right);
            make.width.mas_equalTo(90);
            make.height.mas_equalTo(12);
    }];
    
    
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(200);
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.countBtn.mas_bottom).offset(35);
    }];
    
    [self.daixieLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.right.mas_equalTo(10);
        make.top.mas_equalTo(0);
    }];
    [self.bmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.daixieLabel.mas_height);
        make.left.right.mas_equalTo(10);
        make.top.mas_equalTo(self.daixieLabel.mas_bottom);
    }];
    [self.heahlthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.daixieLabel.mas_height);
        make.left.right.mas_equalTo(10);
        make.top.mas_equalTo(self.bmLabel.mas_bottom);
    }];
    [self.xinlvLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.daixieLabel.mas_height);
        make.left.right.mas_equalTo(10);
        make.top.mas_equalTo(self.heahlthLabel.mas_bottom);
    }];
    [self.beLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.daixieLabel.mas_height);
        make.left.right.mas_equalTo(10);
        make.top.mas_equalTo(self.xinlvLabel.mas_bottom);
    }];
 
    [self.fromView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(355);
        make.top.mas_equalTo(self.contentView.mas_bottom).offset(10);
        make.height.mas_equalTo(175);
        make.centerX.mas_equalTo(self.view.mas_centerX);
    }];
}

- (void)requestData{
   
    WeakSelf(self);
    [YGBodyDataBLL requestServerDetailWithData:self.ageTextField.text sex:self.sex stature:self.heightTextField.text weight:self.weightTextField.text Success:^(YGBodyDataModel * _Nonnull model) {
        weakself.daixieLabel.attributedText = [weakself getAttStringWithContent:defaultDXContent title:model.bmr subStr:@"大卡"];
        weakself.bmLabel.attributedText = [weakself getAttStringWithContent:defaultBMContent title:model.bmr subStr:@""];
        weakself.heahlthLabel.attributedText = [weakself getAttStringWithContent:defaultBMContent title:[NSString stringWithFormat:@"%@~%@",model.frontStandardStature,model.afterStandardStature] subStr:@"kg"];
        weakself.xinlvLabel.attributedText = [weakself getAttStringWithContent:defaultXLContent title:[NSString stringWithFormat:@"%@~%@",model.middleHeartRate,model.lowHeartRate] subStr:@"次/分钟"];
        weakself.beLabel.attributedText = [weakself getAttStringWithContent:defaultBEContent title:model.bee subStr:@"kcal"];

    } fail:^(NSError * _Nonnull error) {
        
    }];
}

#pragma event

-(void)clickMaleBtnAction{
    self.femaleBtn.selected = NO;
    self.maleBtn.selected = YES;
    self.sex = @"1";
}

-(void)clickFemaleBtnAction{
    self.femaleBtn.selected = YES;
    self.maleBtn.selected = NO;
    self.sex = @"2";

}

-(void)clickCountBtnAction{
    [self requestData];
}

-(void)clickHistoryBtnAction{
    YGBodyHistoryViewController *vc = [[YGBodyHistoryViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma setter

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text =@"请输入您的性别、年龄、身高和体重";
        _titleLabel.textColor = UIColorFromRGB(0x333333);
        _titleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _titleLabel;
}

- (UILabel *)genderLabel{
    if (!_genderLabel) {
        _genderLabel = [[UILabel alloc]init];
        _genderLabel.text = @"性别：";
        _genderLabel.textColor = UIColorFromRGB(0x333333);
        _genderLabel.font = [UIFont systemFontOfSize:13];
    }
    return _genderLabel;
}

- (UIButton *)maleBtn{
    if (!_maleBtn) {
        _maleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_maleBtn addTarget:self action:@selector(clickMaleBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_maleBtn setBackgroundImage:[UIImage imageNamed:@"gender_btn_icon"] forState:UIControlStateNormal];
        [_maleBtn setBackgroundImage:[UIImage imageNamed:@"gender_selected_btn_icon"] forState:UIControlStateSelected];
        _maleBtn.selected = YES;
    }
    return _maleBtn;
}

- (UILabel *)maleLabel{
    if (!_maleLabel) {
        _maleLabel = [[UILabel alloc]init];
        _maleLabel.text = @"男";
        _maleLabel.textColor = UIColorFromRGB(0x333333);
        _maleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _maleLabel;
}

- (UIButton *)femaleBtn{
    if (!_femaleBtn) {
        _femaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_femaleBtn addTarget:self action:@selector(clickFemaleBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_femaleBtn setBackgroundImage:[UIImage imageNamed:@"gender_btn_icon"] forState:UIControlStateNormal];
        [_femaleBtn setBackgroundImage:[UIImage imageNamed:@"gender_selected_btn_icon"] forState:UIControlStateSelected];
        _femaleBtn.selected = NO;
    }
    return _femaleBtn;
}

- (UILabel *)femaleLabel{
    if (!_femaleLabel) {
        _femaleLabel = [[UILabel alloc]init];
        _femaleLabel.text = @"女";
        _femaleLabel.textColor = UIColorFromRGB(0x333333);
        _femaleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _femaleLabel;
}

- (UILabel *)ageLabel{
    if (!_ageLabel) {
        _ageLabel = [[UILabel alloc]init];
        _ageLabel.text = @"年龄：";
        _ageLabel.textColor = UIColorFromRGB(0x333333);
        _ageLabel.font = [UIFont systemFontOfSize:13];
    }
    return _ageLabel;
}

- (UITextField *)ageTextField{
    if (!_ageTextField) {
        _ageTextField = [[UITextField alloc]init];
        _ageTextField.textAlignment = NSTextAlignmentCenter;
        _ageTextField.font = [UIFont systemFontOfSize:13];
        _ageTextField.layer.borderWidth = 0.2;
        _ageTextField.layer.cornerRadius = 5;
        _ageTextField.clipsToBounds = YES;
    }
    return _ageTextField;
}

- (UILabel *)unitAgeLabel{
    if (!_unitAgeLabel) {
        _unitAgeLabel = [[UILabel alloc]init];
        _unitAgeLabel.text = @"岁";
        _unitAgeLabel.textColor = UIColorFromRGB(0x333333);
        _unitAgeLabel.font = [UIFont systemFontOfSize:13];
    }
    return _unitAgeLabel;
}

- (UILabel *)heightLabel{
    if (!_heightLabel) {
        _heightLabel = [[UILabel alloc]init];
        _heightLabel.text = @"身高：";
        _heightLabel.textColor = UIColorFromRGB(0x333333);
        _heightLabel.font = [UIFont systemFontOfSize:13];
    }
    return _heightLabel;
}

- (UITextField *)heightTextField{
    if (!_heightTextField) {
        _heightTextField = [[UITextField alloc]init];
        _heightTextField.textAlignment = NSTextAlignmentCenter;
        _heightTextField.font = [UIFont systemFontOfSize:13];
        _heightTextField.layer.borderWidth = 0.2;
        _heightTextField.layer.cornerRadius = 5;
        _heightTextField.clipsToBounds = YES;
    }
    return _heightTextField;
}

- (UILabel *)unitHeightLabel{
    if (!_unitHeightLabel) {
        _unitHeightLabel = [[UILabel alloc]init];
        _unitHeightLabel.text = @"单位：厘米cm";
        _unitHeightLabel.textColor = UIColorFromRGB(0x333333);
        _unitHeightLabel.font = [UIFont systemFontOfSize:13];
    }
    return _unitHeightLabel;
}

- (UILabel *)weightLabel{
    if (!_weightLabel) {
        _weightLabel = [[UILabel alloc]init];
        _weightLabel.text = @"体重：";
        _weightLabel.textColor = UIColorFromRGB(0x333333);
        _weightLabel.font = [UIFont systemFontOfSize:13];
    }
    return _weightLabel;
}

- (UITextField *)weightTextField{
    if (!_weightTextField) {
        _weightTextField = [[UITextField alloc]init];
        _weightTextField.textAlignment = NSTextAlignmentCenter;
        _weightTextField.font = [UIFont systemFontOfSize:13];
        _weightTextField.layer.borderWidth = 0.2;
        _weightTextField.layer.cornerRadius = 5;
        _weightTextField.clipsToBounds = YES;
    }
    return _weightTextField;
}

- (UILabel *)unitWeightLabel{
    if (!_unitWeightLabel) {
        _unitWeightLabel = [[UILabel alloc]init];
        _unitWeightLabel.text = @"单位：千克kg";
        _unitWeightLabel.textColor = UIColorFromRGB(0x333333);
        _unitWeightLabel.font = [UIFont systemFontOfSize:13];
    }
    return _unitWeightLabel;
}

- (UIButton *)countBtn{
    if (!_countBtn) {
        _countBtn = [[UIButton alloc]init];
        [_countBtn addTarget:self action:@selector(clickCountBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _countBtn.backgroundColor = UIColorFromRGB(0x37C8D7);
        [_countBtn setTitle:@"计算" forState:UIControlStateNormal];
        _countBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        _countBtn.tintColor = [UIColor whiteColor];
        _countBtn.layer.cornerRadius = 5;
        _countBtn.clipsToBounds = YES;
        
    }
    return _countBtn;
}

- (UIButton *)historyBtn{
    if (!_historyBtn) {
        _historyBtn = [[UIButton alloc]init];
        [_historyBtn addTarget:self action:@selector(clickHistoryBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _historyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_historyBtn setTitle:@"查看历史记录" forState:UIControlStateNormal];
        [_historyBtn setTitleColor:UIColorFromRGB(0x37c8d7) forState:UIControlStateNormal];
    }
    return _historyBtn;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

- (UILabel *)daixieLabel{
    if (!_daixieLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = defaultDXContent;
        label.textColor = UIColorFromRGB(0x333333);
        label.font = [UIFont systemFontOfSize:13];
        _daixieLabel = label;
    }
    return _daixieLabel;
}

- (UILabel *)bmLabel{
    if (!_bmLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = defaultBMContent;
        label.textColor = UIColorFromRGB(0x333333);
        label.font = [UIFont systemFontOfSize:13];
        _bmLabel = label;
    }
    return _bmLabel;
}

- (UILabel *)heahlthLabel{
    if (!_heahlthLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = defaultJKContent;
        label.textColor = UIColorFromRGB(0x333333);
        label.font = [UIFont systemFontOfSize:13];
        _heahlthLabel = label;
    }
    return _heahlthLabel;
}

- (UILabel *)xinlvLabel{
    if (!_xinlvLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = defaultXLContent;
        label.textColor = UIColorFromRGB(0x333333);
        label.font = [UIFont systemFontOfSize:13];
        _xinlvLabel = label;
    }
    return _xinlvLabel;
}

- (UILabel *)beLabel{
    if (!_beLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.text = defaultBEContent;
        label.textColor = UIColorFromRGB(0x333333);
        label.font = [UIFont systemFontOfSize:13];
        _beLabel = label;
    }
    return _beLabel;
}

- (YGFormTableView *)fromView{
    if (!_fromView) {
        YGFromTableConfigure *confi = [[YGFromTableConfigure alloc] init];
        confi.row = 2;
        YGFromHeaderConfigure *headerConfi = [[YGFromHeaderConfigure alloc] init];
        confi.cellHieght = 35;
        headerConfi.row = 1;
        confi.headerConfigure = headerConfi;
        _fromView = [[YGFormTableView alloc] initWithConfigure:confi registerCell:YGBodyDataTableViewCell.class];
    }
    return _fromView;
}

- (NSMutableAttributedString *)getAttStringWithContent:(NSString *)content title:(NSString *)title subStr:(NSString *)subStr{
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] init];
    
    NSMutableAttributedString *contentAtt = [[NSMutableAttributedString alloc] initWithString:content attributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x333333)}];
    
    NSMutableAttributedString *titleAtt = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x37C8D7)}];

    NSMutableAttributedString *subStrAtt = [[NSMutableAttributedString alloc] initWithString:subStr attributes:@{NSForegroundColorAttributeName: UIColorFromRGB(0x333333)}];

    [att appendAttributedString:contentAtt];
    [att appendAttributedString:titleAtt];
    [att appendAttributedString:subStrAtt];

    return att;

}

- (NSArray *)getFormData{
    
    NSArray *dataArray = @[@[@"成年人身体质量指数"],
                           @[@"轻体重",@"BMI<18.5"],
                           @[@"健康体重BMI",@"18.5<=BMI<=24"],
                           @[@"超重体重BMI",@"24<=BMI<=28"],
                           @[@"肥胖BMI",@"28<=BMI"]];
    return dataArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
